import java.net.*;
import java.io.*;
import java.util.*;

/** ServerTalk1.java and LocalTalk1.java :
   The single client-server program has the following features:
	1. The server accepts only one connection and talks to
	   only one client.
	2. InputStream is used to send and receive bytes through
	   the socket.
	3. The number of bytes read through the socket is determined
	   by both the number of bytes in the socket and the array
	   size defined.
*/

public class ServerTalk1 {
   public static void main(String args[] ) {

     InputStream in;
     OutputStream out;
     String message;
     int len;
     byte bytes[] = new byte[30];

     try {
 	ServerSocket ssk = new ServerSocket(8900);
	int anum = 1;
	Socket       sk = ssk.accept();

	in  = sk.getInputStream();
	out = sk.getOutputStream();

	while ( true ) {
		message = "Server message: " + anum ++;
		out.write( message.getBytes());

		if ( in.available() > 0 ) {
			len = in.read( bytes);
			System.out.println(new String(bytes, 0, len));
		}
		
		Thread.sleep( (int)(Math.random() * 2000) + 500);
	}
      } catch (Exception e) { e.printStackTrace(); }
   }
}

